package com.generator.controller.article;

import com.generator.dto.article.ArticleAuditDto;
import com.generator.dto.article.ArticleDto;
import com.generator.service.article.ArticleService;
import org.apache.ibatis.annotations.Param;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/generate")
@CrossOrigin(allowCredentials = "true")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @GetMapping("/findAllArticle")
   public List<ArticleDto> findAllArticle(){
        return articleService.findAllArticle();
    }

    @GetMapping("/findArticleById")
    public ArticleDto findArticleById(int articleId){
        return articleService.findArticleById(articleId);
    }

    @PostMapping("/updateArticle")
    public void updateArticle(@RequestBody ArticleDto articleDto){
        Date now = new Date();
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(now);
        cal1.set(Calendar.HOUR_OF_DAY, 0);
        cal1.set(Calendar.MINUTE, 0);
        cal1.set(Calendar.SECOND, 0);
        cal1.set(Calendar.MILLISECOND, 0);
        articleDto.setCrateTime(cal1.getTime());
        articleService.updateArticle(articleDto);
    }

    @GetMapping("/delArticle")
    public void delArticle(int articleId){
        articleService.delArticle(articleId);
    }

    @GetMapping("/addArticle")
    public void addArticle(@RequestBody ArticleDto articleDto){
        articleService.addArticle(articleDto);
    }

    @GetMapping("/auditOpinions")
    public List<ArticleAuditDto> auditOpinions(){
        return articleService.auditOpinions();
    }

    @GetMapping("/updateOpinion")
    public void updateOpinion(ArticleAuditDto articleAuditDto){
         articleService.updateOpinion(articleAuditDto);
    }

    @GetMapping("/findOpinionById")
    public ArticleAuditDto findOpinionById(Integer articleId){
        return articleService.findOpinionById(articleId);
    }

}
